package com.nf.health.app.mine.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.customview.MyFlowLayout;
import com.nf.health.app.http.Act;
import com.nf.health.app.utils.JsonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author hepan
 * @ClassName: DiseaseHistoryActivity
 * @Description:病史
 * @date 2015-11-4 上午9:48:15
 * <p>
 * { "高血压", "糖尿病", "肥胖", "血脂异常", "便秘", "失眠", "冠心病", "慢阻肺", "脑血管病后遗症", "颈椎病", "月经不调", "骨质疏松" };
 * <p>
 * {"value":"糖尿病,肥胖,血脂异常","other":"其他病"}
 */
public class DiseaseKindsActivity extends BaseActivity implements
        OnClickListener {

    @Bind(R.id.titlebar_title_tv)
    TextView titlebarTitleTv;
    @Bind(R.id.titlebar_right_tv)
    TextView titlebarRightTv;
    @Bind(R.id.fl_disease)
    MyFlowLayout flDisease;
    @Bind(R.id.fl_selected_disease)
    MyFlowLayout flSelectedDisease;
    @Bind(R.id.tv_remind)
    TextView tvRemind;
    @Bind(R.id.et_other_disease)
    EditText etOtherDisease;

    private Random random = new Random();
    private int[] colors = new int[]{R.color.theme_color, R.color.orange, R.color.holo_blue_bright,
            R.color.home_day, R.color.holo_green_light, R.color.blue5};
    private int colorsLength = 6;
    private List<String> mDieaseList;
    private List<String> mSelectList = new ArrayList<>();

    private String mHistory;//现有疾病历史

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_dieases_kinds);
        ButterKnife.bind(this);
        mHistory = getIntent().getStringExtra(Constants.INTENT_EXTRA_STRING);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        titlebarTitleTv.setText("病史");
        titlebarRightTv.setVisibility(View.VISIBLE);
        titlebarRightTv.setText("更改病史");
        if (!TextUtils.isEmpty(mHistory)) {
            String value = JsonUtils.getKey2Value(mHistory, "value");
            String other = JsonUtils.getKey2Value(mHistory, "other");
            //初始化选中的常见疾病
            if (!TextUtils.isEmpty(value) && !"失败".equals(value)) {
                String values[] = value.split(",");
                for (int i = 0; i < values.length; i++) {
                    mSelectList.add(values[i]);
                    TextView tv = (TextView) View.inflate(this, R.layout.item_selected_disease, null);
                    ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(+ViewGroup.LayoutParams.WRAP_CONTENT, +ViewGroup.LayoutParams.WRAP_CONTENT);
                    tv.setText(values[i]);
                    marginLayoutParams.setMargins(10, 10, 10, 10);
                    tv.setGravity(Gravity.CENTER);
                    tv.setTextColor(getResources().getColor(colors[random.nextInt(colorsLength)]));
                    tv.setOnClickListener(this);
                    tv.setTag("selected");
                    flSelectedDisease.addView(tv, marginLayoutParams);
                    if (flSelectedDisease.getChildCount() > 0) {
                        tvRemind.setVisibility(View.GONE);
                    }
                }
            }
            //初始化其他疾病
            if (!TextUtils.isEmpty(other) && !"失败".equals(other)){
                etOtherDisease.setText(other);
                etOtherDisease.setSelection(other.length());
            }
        }
    }

    private void initData() {
        provider.getDiseaseKinds();
    }

    private void initListener() {
        titlebarRightTv.setOnClickListener(this);
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (Act.Flag.GET_DISEASE_KINDS_FLAG.equals(actionName)) {
            mDieaseList = (List<String>) object;
            flDisease.removeAllViews();
            for (int i = 0; i < mDieaseList.size(); i++) {
                TextView tv = (TextView) View.inflate(this, R.layout.item_disease_name, null);
                ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(+ViewGroup.LayoutParams.WRAP_CONTENT, +ViewGroup.LayoutParams.WRAP_CONTENT);
                tv.setText(mDieaseList.get(i));
                marginLayoutParams.setMargins(10, 10, 10, 10);
                tv.setGravity(Gravity.CENTER);
                tv.setTextColor(getResources().getColor(colors[random.nextInt(colorsLength)]));
                tv.setOnClickListener(this);
                tv.setTag(i);
                flDisease.addView(tv, marginLayoutParams);
            }
        } else if (Act.Flag.UPDATE_DISEASE_KINDS_FLAG.equals(actionName)) {
//            showToast("更新成功");
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        Object obj = v.getTag();
        if (v instanceof TextView && obj instanceof Integer) {//底部疾病
            String name = mDieaseList.get((Integer) obj);
            if (mSelectList.contains(name)) {
                showToast("不可重复添加");
            } else {
                mSelectList.add(name);
                TextView tv = (TextView) View.inflate(this, R.layout.item_selected_disease, null);
                ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(+ViewGroup.LayoutParams.WRAP_CONTENT, +ViewGroup.LayoutParams.WRAP_CONTENT);
                tv.setText(name);
                marginLayoutParams.setMargins(10, 10, 10, 10);
                tv.setGravity(Gravity.CENTER);
                tv.setTextColor(getResources().getColor(colors[random.nextInt(colorsLength)]));
                tv.setOnClickListener(this);
                tv.setTag("selected");
                flSelectedDisease.addView(tv, marginLayoutParams);
                if (flSelectedDisease.getChildCount() > 0) {
                    tvRemind.setVisibility(View.GONE);
                }
            }
        } else if (v instanceof TextView && obj instanceof String) {
            //移除选中的内容
            flSelectedDisease.removeView(v);
            mSelectList.remove(((TextView) v).getText().toString());
            if (flSelectedDisease.getChildCount() == 0) {
                tvRemind.setVisibility(View.VISIBLE);
            }
        } else if (v.getId() == R.id.titlebar_right_tv) {
            updateDiseaseKinds();
        }
    }

    /**
     * 更新疾病史
     */
    private void updateDiseaseKinds() {
        //获得选择
        String value = "";
        for (int i = 0; i < mSelectList.size(); i++) {
            value += mSelectList.get(i);
            if (i != mSelectList.size() - 1) {
                value += ",";
            }
        }
        String other = etOtherDisease.getText().toString();
        HashMap<String, String> map = new HashMap<>();
        map.put("value", value);
        map.put("other", other);
        provider.requestUpdateUser(null, null, null, null, null, new MyGsonBuilder().createGson().toJson(map), null, null,null, Act.Flag.UPDATE_DISEASE_KINDS_FLAG);
    }
}
